package cn.com.nes.common.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.UserInfo;
import cn.com.nes.site.module.dao.ProjectDaoImpl;

public class SyncData {

	public static void main(String[] args) throws SQLException {
		Connection conn = null ;
		try {
			List<LinkedHashMap<String, String>> list = getInstallData() ;
			conn = DBUtils.getJdbcForDB() ;
			
			for(int i=0;i<list.size();i++) {
				LinkedHashMap<String, String> e = list.get(i) ;
				inertProject(conn,e) ;
				conn.commit();
			}
		}finally {
			if(conn!=null) conn.close();
		}

		
	}
	
	//项目排期
	public static void inertProjectScheduling(Connection conn,String project_uuid,LinkedHashMap<String, String> data) throws SQLException {
		try {
			ProjectDaoImpl impl = new ProjectDaoImpl() ;
			
			
			DataReq req = new DataReq() ;
			UserInfo uinfo = new UserInfo();
			uinfo.setCompanyId("0000");
			uinfo.setId("131");
			uinfo.setUsername("管理员");
			uinfo.setNes_companyId("MA3NU7QX1");
			req.setUinfo(uinfo );
			HashMap<String, Object> reqData = new LinkedHashMap<String, Object>();
			reqData.put("jsondata", "[]") ;
			reqData.put("createUid", uinfo.getId()) ;
			reqData.put("modifyUid", uinfo.getId()) ;
			reqData.put("createTime", DBUitl.getDateMinute(data.get("cjsj"), 58)) ;
			reqData.put("modifyTime", data.get("cjsj")) ;

			reqData.put("project_uuid", project_uuid) ;
			reqData.put("start_time" , data.get("gmt_create")) ;
			reqData.put("end_time",  data.get("gmt_create")) ;
			reqData.put("department_id", "00") ;
			req.setReqData(reqData );
			PageData res = impl.addProjectScheduling(conn, req );
			
			data.put("work_order_id", (String)res.getExtData()) ;
			System.out.println(JsonUtil.getStriFromObj(res));
		}finally {
		}
	}
	
	public static void changeOrderDtatus(Connection conn,String project_uuid,LinkedHashMap<String, String> data) throws SQLException {
		try {
			ProjectDaoImpl impl = new ProjectDaoImpl() ;
			
			
			DataReq req = new DataReq() ;
			UserInfo uinfo = new UserInfo();
			uinfo.setCompanyId("0000");
			uinfo.setId("131");
			uinfo.setUsername("管理员");
			uinfo.setNes_companyId("MA3NU7QX1");
			req.setUinfo(uinfo );
			HashMap<String, Object> reqData = new LinkedHashMap<String, Object>();
			reqData.put("jsondata", "[]") ;
			reqData.put("createUid", uinfo.getId()) ;
			reqData.put("modifyUid", uinfo.getId()) ;
			reqData.put("createTime",DBUitl.getDateMinute(data.get("cjsj"), Math.round((58*Integer.parseInt(data.get("status")))))) ;
			reqData.put("modifyTime", data.get("cjsj")) ;
			
			reqData.put("isOUT", "1") ;

			reqData.put("longitude",  data.get("jd")) ;
			reqData.put("latitude",  data.get("wd")) ;
			reqData.put("project_uuid", project_uuid) ;
			reqData.put("status" , data.get("status")) ;
			reqData.put("start_time" , data.get("gmt_create")) ;
			reqData.put("end_time",  data.get("gmt_create")) ;
			reqData.put("work_order_id",  data.get("work_order_id")) ;
			reqData.put("department_id", "00") ;
			req.setReqData(reqData );
			PageData res = impl.changeOrderStatus(conn, req );
			System.out.println(JsonUtil.getStriFromObj(res));
		}finally {
		}
	}	
	
	public static void updateProjectAuditStatus(Connection conn,String project_uuid,LinkedHashMap<String, String> data) throws SQLException {
		try {
			ProjectDaoImpl impl = new ProjectDaoImpl() ;
			
			
			DataReq req = new DataReq() ;
			UserInfo uinfo = new UserInfo();
			uinfo.setCompanyId("0000");
			uinfo.setId("131");
			uinfo.setUsername("管理员");
			uinfo.setNes_companyId("MA3NU7QX1");
			req.setUinfo(uinfo );
			HashMap<String, Object> reqData = new LinkedHashMap<String, Object>();
			reqData.put("jsondata", "[]") ;
			reqData.put("createUid", uinfo.getId()) ;
			reqData.put("modifyUid", uinfo.getId()) ;
			reqData.put("createTime", DBUitl.getDateMinute(data.get("cjsj"), Math.round((58*53)))) ;
			reqData.put("modifyTime", data.get("cjsj")) ;		
			
			reqData.put("auditStatus",  data.get("auditStatus")) ;
			reqData.put("auditComments",  data.get("auditComments")) ;
			reqData.put("project_uuid", project_uuid) ;
			reqData.put("work_order_id",  data.get("work_order_id")) ;
			
			req.setReqData(reqData );
			PageData res = impl.updateProjectAuditStatus(conn, req );
			System.out.println(JsonUtil.getStriFromObj(res));
		}finally {
		}
	}	
	
	
	public static void inertProject(Connection conn,LinkedHashMap<String, String> data) throws SQLException {
		try {
			ProjectDaoImpl impl = new ProjectDaoImpl() ;
			DataReq req = new DataReq() ;
			UserInfo uinfo = new UserInfo();
			uinfo.setCompanyId("0000");
			uinfo.setId("131");
			uinfo.setUsername("管理员");
			uinfo.setNes_companyId("779975230");
			req.setUinfo(uinfo );
			HashMap<String, Object> reqData = new LinkedHashMap<String, Object>();
			reqData.put("jsondata", "[]") ;
			

			reqData.put("project_id", data.get("xmbh")) ;
			reqData.put("project_name", data.get("xmmc")) ;
			reqData.put("planned_start_time" , data.get("gmt_create")) ;
			reqData.put("planned_end_time",  data.get("gmt_create")) ;
			reqData.put("investment_scale", null) ;
			reqData.put("area_code",  data.get("xzqh")) ;
			reqData.put("company_id", uinfo.getCompanyId()) ;
			reqData.put("charge", data.get("xmlxr")) ;
			reqData.put("service_mode", "01") ;
			reqData.put("project_status", "01") ;
			reqData.put("project_type", "01") ;
			reqData.put("project_level", "05") ;
			reqData.put("contact", data.get("xmlxr")) ;
			reqData.put("contact_phone", data.get("xmlxrdh")) ;
			reqData.put("project_address", data.get("xmdz")) ;
			reqData.put("longitude",  data.get("jd")) ;
			reqData.put("latitude",  data.get("wd")) ;
			reqData.put("images", "") ;
			
			reqData.put("createUid", uinfo.getId()) ;
			reqData.put("modifyUid", uinfo.getId()) ;
			reqData.put("createTime", data.get("cjsj")) ;
			reqData.put("modifyTime", data.get("cjsj")) ;
			reqData.put("manufacture_id", uinfo.getNes_companyId()) ;
			reqData.put("ope_manufacture_id", uinfo.getNes_companyId()) ;
			reqData.put("department_id", "00") ;
			
			req.setReqData(reqData );
			PageData res = impl.addProject(conn, req );
			System.out.println(JsonUtil.getStriFromObj(res));
			String project_uuid = res.getKey() ;

			
			//插入排期数据
			inertProjectScheduling(conn,project_uuid,data);
			
			//插入派单数据
			data.put("status", "02") ;
			data.put("describe", "派遣工单") ;
			data.put("contact", data.get("xmlxr")) ;
			data.put("contact_phone", data.get("xmlxrdh")) ;
			data.put("maintain_id", "457") ;
			data.put("planned_start_time",data.get("cjsj")) ;
			data.put("planned_end_time", data.get("cjsj")) ;

			changeOrderDtatus(conn,project_uuid,data);
			
		    //用户接单
			data.put("status", "04") ;
			data.put("describe", "已接单") ;
			req.getReqData().put("maintain_id", req.getUinfo().getId()) ;
			changeOrderDtatus(conn,project_uuid,data);
			
			//已到达
			data.put("status", "05") ;
			data.put("describe", "已到达") ;
			
			req.getReqData().put("maintain_id", req.getUinfo().getId()) ;
			changeOrderDtatus(conn,project_uuid,data);
			
			
			data.put("status", "06") ;
			data.put("describe", "已完成") ;
			req.getReqData().put("maintain_id", req.getUinfo().getId()) ;
			changeOrderDtatus(conn,project_uuid,data);
			
			//验收
			data.put("auditStatus", "1") ;
			data.put("auditComments", "同意验收") ;
			data.put("project_uuid", project_uuid) ;
			updateProjectAuditStatus(conn,project_uuid,data);
			
			
		}finally {
		}
	}	
	
	
	public static List<LinkedHashMap<String, String>> getInstallData() throws SQLException {
		List<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String, String>>();
		Connection conn = null ;
		PreparedStatement ps = null ;
        StringBuffer sb = new StringBuffer();
        ResultSet rs = null ;
		try {
			conn = DBUtils.getJdbcForOpe();
			sb.append("SELECT a.dd_id,a.xmbh,a.xmdz,a.xzqh,a.xmlxr,a.xmlxrdh,a.xmmc,a.jd,a.wd,b.zbq,b.cjsj,date_format(b.cjsj,'%Y-%m-%d') gmt_create  from is_install_info a INNER JOIN is_equipment_info b on a.dd_id=b.dd_id where b.manufacture_id='779975230' and  b.dd_id like '110116%' and b.sfsc='0' and a.jd is not null limit 1000 ") ;
			ps = conn.prepareStatement(sb.toString());
        	rs = ps.executeQuery();
        	ResultSetMetaData rsmd = rs.getMetaData();
			int columnCount = rsmd.getColumnCount();

			
			while (rs.next()) {
				LinkedHashMap<String, String> map = null;
				map = new LinkedHashMap<String, String>();
				for (int k = 1; k <= columnCount; k++) {
					map.put(rsmd.getColumnLabel(k),
							rs.getString(k) == null ? "" : rs.getString(k));
				}
				list.add(map);
			}
		}finally {
			if(conn!=null) conn.close();
		}
		
		return list ;
		
	}

}
